﻿
using System.Text;
using System.Text.RegularExpressions;

namespace NoNameGoServer.Client.Extensions {
    public static class StringBuilderExtension {
        public static string ReadLine(this StringBuilder sb) {
            StringBuilder result = new StringBuilder();
            for (int i = 0; i < sb.Length; i++) {
                char ch = sb[i];
                if (ch == '\r') {
                    continue;
                }
                if (ch == '\n') {
                    sb.Remove(0, i + 1);
                    return result.ToString();
                }
                result.Append(ch);
            }
            return null;
        }

        public static bool RemoveRegEx(this StringBuilder sb, string pattern, out string[] groups) {
            string input = sb.ToString();
            RegexOptions options = RegexOptions.Multiline;
            Match match = Regex.Match(input, pattern, options);
            if (match.Success) {
                groups = new string[match.Groups.Count];
                for (int i = 0; i < match.Groups.Count; i++) {
                    groups[i] = match.Groups[i].Value;
                }
                sb.Remove(match.Index, match.Length);
                return true;
            }
            groups = null;
            return false;
        }
    }
}
